package com.example.demo.mapper;

import com.example.demo.entity.DTO.GoodsDTO;
import com.example.demo.entity.DTO.GoodsNewDTO;
import com.example.demo.entity.Goods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.WorkersAndBookedTime;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 Mapper 接口
 * </p>
 *
 * @author Mr.Xiayh
 * @since 2024-06-04
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * @param goods ''
     * @return List<Goods>
     */
    public List<Goods> selectByCriteria(Goods goods);


    @Select("select * from goods")
    List<GoodsDTO> findAllByCriteria();

    /**
     * 添加商品
     * @param goods
     * @return
     */
    boolean insertGoods(Goods goods);
    /**
     * 查询商品对象
     */

    @Select("select * from an_nideshop_good where id = #{value}")
    GoodsDTO queryObject(@Param("value")Integer id);
    @Select("<script>" +
            "select" +
            "        <if test='fields != null and fields != \"\"'>" +
            "            ${fields}" +
            "        </if>" +
            "        <if test='fields == null or fields == \"\"'>" +
            "            *" +
            "        </if>" +
            "        from an_nideshop_good" +
            "        where 1 = 1" +
            "        <if test='isNew != null and isNew != \"\"'>" +
            "            and is_new = #{isNew}" +
            "        </if>" +
            "        <if test='isHot != null and isHot != \"\"'>" +
            "            and is_hot = #{isHot}" +
            "        </if>" +
            "        <if test='brandId != null and brandId != \"\"'>" +
            "            and brand_id = #{brandId}" +
            "        </if>" +
            "        <if test='name != null and name != \"\"'>" +
            "            and name like concat('%',#{name},'%')" +
            "        </if>" +
            "        <if test='categoryId != null and categoryId != \"\"'>" +
            "            and category_id = #{categoryId}" +
            "        </if>" +
            "        <if test='keyword != null'>" +
            "            and keywords like concat('%',#{keyword},'%')" +
            "        </if>" +
            "        <if test='isDelete != null'>" +
            "            and is_delete = #{isDelete}" +
            "        </if>" +
            "        <if test='isOnSale != null and isOnSale != \"\"'>" +
            "            and is_on_sale = #{isOnSale}" +
            "        </if>" +
            "        <if test='categoryIds != null'>" +
            "            and category_id in" +
            "            <foreach item='item' collection='categoryIds' open='(' separator=',' close=')'>" +
            "                #{item}" +
            "            </foreach>" +
            "        </if>" +
            "        <if test='goodsIds != null'>" +
            "            and id in" +
            "            <foreach item='item' collection='goodsIds' open='(' separator=',' close=')'>" +
            "                #{item}" +
            "            </foreach>" +
            "        </if>" +
            "        <choose>" +
            "            <when test='sidx != null and sidx.trim() != \"\"'>" +
            "                order by ${sidx} ${order}" +
            "            </when>" +
            "            <otherwise>" +
            "                order by id desc" +
            "            </otherwise>" +
            "        </choose>" +
            "</script>")
    List<GoodsNewDTO> queryList(Map<String, Object> param);

    @Select("<script>" +
            "select a.id, a.name, a.list_pic_url, a.market_price, a.retail_price, a.goods_brief, b.id AS" +
            "        product_id,count(og.id) as order_num" +
            "        from an_nideshop_good a" +
            "        left join af_nideshop_product b on a.id = b.goods_id" +
            "        left join af_nideshop_product c on a.id = c.goods_id and c.retail_price > b.retail_price and b.id != c.id" +
            "        left join AFshop_order_goods og on og.product_id = b.id" +
            "        <if test='is_group != null and is_group == true'>" +
            "            left join nideshop_goods_group gg on gg.goods_id = a.id" +
            "        </if>" +
            "        where 1 = 1 and b.id is not null and c.id is null and a.is_delete != 1" +
            "        <if test='isNew != null and isNew != \"\"'>" +
            "            and a.is_new = #{isNew}" +
            "        </if>" +
            "        <if test='isHot != null and isHot != \"\"'>" +
            "            and a.is_hot = #{isHot}\n" +
            "        </if>" +
            "        <if test='brandId != null and brandId != \"\"'>" +
            "            and a.brand_id = #{brandId}" +
            "        </if>" +
            "        <if test='name != null and name != \"\"'>" +
            "            and a.name like concat('%',#{name},'%')" +
            "        </if>" +
            "        <if test='categoryId != null and categoryId != \"\"'>" +
            "            and a.category_id = #{categoryId}" +
            "        </if>" +
            "        <if test='keyword != null'>" +
            "            and a.keywords like concat('%',#{keyword},'%')" +
            "        </if>" +
            "        <if test='is_group != null and is_group == true'>" +
            "            and gg.id is not null" +
            "        </if>" +
            "        <if test='categoryIds != null'>" +
            "            and a.category_id in" +
            "            <foreach item='item' collection='categoryIds' open='(' separator=',' close=')'>" +
            "                #{item}" +
            "            </foreach>" +
            "        </if>" +
            "        <if test='goodsIds != null'>" +
            "            and a.id in" +
            "            <foreach item='item' collection='goodsIds' open='(' separator=',' close=')'>" +
            "                #{item}" +
            "            </foreach>" +
            "        </if>" +
            "        group by a.id, a.name, a.list_pic_url, a.market_price, a.retail_price, a.goods_brief, b.id" +
            "        <choose>" +
            "            <when test='sidx != null and sidx.trim() != \"\"'>" +
            "                order by ${sidx} ${order}" +
            "            </when>" +
            "            <otherwise>" +
            "                order by a.id desc" +
            "            </otherwise>" +
            "        </choose>" +
            "        <if test='offset != null and limit != null'>" +
            "            limit #{offset}, #{limit}" +
            "        </if>" +
            "</script>")
    List<GoodsNewDTO> queryCatalogProductList(Map<String, Object> params);
    @Select("<script>" +
            "select count(*) from an_nideshop_good" +
            "    where 1 = 1" +
            "        <if test='isDelete != null'>" +
            "            and is_delete = #{isDelete}" +
            "        </if>" +
            "        <if test='isOnSale != null and isOnSale != \"\"'>" +
            "            and is_on_sale = #{isOnSale}" +
            "        </if>" +
            "</script>")
    int queryTotal(Map<String, Object> map);
}
